//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System.Windows.Forms;
using GuidanceExplorer.Model;
using GuidanceExplorer.Model.Conditions;

namespace GuidanceExplorer.Controls
{
    /// <summary>
    /// The node for a single library.
    /// </summary>
    [MenuItemAttribute(500, "context\\New Item", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    [MenuItemAttribute(505, "context\\New Item Template", GuidanceExplorer.Commands.NewGuidanceItem, typeof(ModelItemMenuItemIsEditableCondition), typeof(NewItemMenuItemProvider), Keys.None)]
    [MenuItemAttribute(510, "context\\-", null, typeof(OrGenericMenuItemCondition<ModelItemMenuItemIsEditableCondition, MenuItemUnderSolutionNodeCondition>), Keys.None)]
    //[MenuItemAttribute(520, "context\\Import Item...", GuidanceExplorer.Commands.ImportGuidanceItemToView, typeof(AndGenericMenuItemCondition<MenuItemNotUnderSolutionNodeCondition, ModelItemMenuItemIsEditableCondition>), Keys.None)]
    [MenuItemAttribute(520, "context\\Import Item...", GuidanceExplorer.Commands.ImportGuidanceItemToView, typeof(AndGenericMenuItemCondition<NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>, ModelItemMenuItemIsEditableCondition>), Keys.None)]
    //[MenuItemAttribute(521, "context\\Save View as...", GuidanceExplorer.Commands.SaveViewAs, Keys.None)]
    [MenuItemAttribute(530, "context\\Export as Library...", GuidanceExplorer.Commands.ExportViewAsLibrary, typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
    [MenuItemAttribute(535, "context\\Export Checklist...", GuidanceExplorer.Commands.ExportSolutionModelItem, typeof(MenuItemUnderSolutionNodeCondition), Keys.None)]
    [MenuItemAttribute(540, "context\\-", null, Keys.None)]
    [MenuItemAttribute(550, "context\\Rename Checklist", GuidanceExplorer.Commands.RenameChecklist, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    [MenuItemAttribute(560, "context\\Delete Checklist", GuidanceExplorer.Commands.DeleteChecklist, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    //[MenuItemAttribute(570, "context\\-", null, typeof(WebSubscribedLibraryCondition), Keys.None)]
    //[MenuItemAttribute(575, "context\\Synchronize Library", Commands.Synchronize, typeof(WebSubscribedLibraryCondition), Keys.None)]
#if ONLINEENABLED
    [MenuItemAttribute(580, "context\\Remove (experimental)", Commands.RemoveView, typeof(WebSubscribedLibraryCondition), Keys.None)]
    [MenuItemAttribute(590, "context\\Publish (experimental)...", GuidanceExplorer.Commands.PublishView, Keys.None)]
    //[MenuItemAttribute(595, "context\\SSE Synchronize (experimental)", Commands.SSESynchronize, Keys.None)]
#endif
    public class ChecklistNode : ModelNode<Checklist>
    {
        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static ChecklistNode New()
        {
            return new ChecklistNode();
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        private ChecklistNode() : base("<a checklist>", 8, 8) { }

        /// <summary>
        /// Whether the node can be dropped.
        /// </summary>
        public override bool CanDrop(IModelItem modelNode)
        {
            Library library = ModelInstance.GetContainerLibrary();
            GuidanceItem guidanceItem = modelNode as GuidanceItem;
            GuidanceItemCollection guidanceItemCollection = modelNode as GuidanceItemCollection;
			GuidanceExplorer.Model.View view = modelNode as GuidanceExplorer.Model.View;
			ViewNode viewNode = modelNode as ViewNode;
			FolderNode folderNoder = modelNode as FolderNode;
			SearchViewNode searchView = modelNode as SearchViewNode;
			GuidanceItemNode itemNode = modelNode as GuidanceItemNode;

			MenuItemUnderSolutionNodeCondition isUnderSolutionNode = new MenuItemUnderSolutionNodeCondition();
			
            if (guidanceItem != null) //TODO: filter only checklist items
            {
                if (library == null)
                {
                    return true;
                }
                else if (library.ReadProtection == ReadProtection.ReadWrite)
                {
                    return guidanceItem.Library == library;
                }
                return false;
            }
			else if (guidanceItemCollection != null)
			{
				foreach (GuidanceItem item in guidanceItemCollection)
				{
					if (library != null)
					{
						if (library.ReadProtection == ReadProtection.ReadOnly)
						{
							return false;
						}
						else if (item.Library != library)
						{
							return false;
						}
					}
				}
				return true;
			}
			else if (itemNode != null)
			{
				if (isUnderSolutionNode.ShowMenuItem(null, itemNode) && !isUnderSolutionNode.ShowMenuItem(null, this))
				{
					return false;
				}

				return true;

			}
			else if (viewNode != null)
			{
				if (isUnderSolutionNode.ShowMenuItem(null, viewNode)) return false;
				
				return true;
			}
			else if (folderNoder != null)
			{
				if (isUnderSolutionNode.ShowMenuItem(null, folderNoder) && isUnderSolutionNode.ShowMenuItem(null, this)) return false;
				return true;
			}
			else if (searchView != null)
			{
				return true;
			}
			else
			{
				//Item type not supported.
				return false;
			}
        }

        /// <summary>
        /// Whether the node is editable.
        /// </summary>
        public override bool IsEditable
        {
            get
            {
                Library library = ModelInstance.GetContainerLibrary();
                if (library != null)
                {
                    return (library.ReadProtection == ReadProtection.ReadWrite);
                }
                return true;
            }
        }

    }
}

